Skip to content

Conversation

geographika
Copy link
Contributor

Overview

As seen during the Diving into pygeoapi workshop the JSON coverage output was broken due to NaN values in the output.

error

This PR ensures NaN are converted to Python None values, which are then converted to null values in the JSON output, using the same approach as in the xarray driver - https://github.com/geopython/pygeoapi/blob/master/pygeoapi/provider/xarray_.py#L395-L398

image

Dependency policy (RFC2)

  • [X ] I have ensured that this PR meets RFC2 requirements

Contributions and licensing

(as per https://github.com/geopython/pygeoapi/blob/master/CONTRIBUTING.md#contributions-and-licensing)

  • I'd like to contribute this bugfix to pygeoapi. I confirm that my contributions to pygeoapi will be compatible with the pygeoapi license guidelines at the time of contribution
  • I have already previously agreed to the pygeoapi Contributions and Licensing Guidelines

@tomkralidis tomkralidis self-requested a review July 20, 2025 09:25
@tomkralidis tomkralidis added this to the 0.21.0 milestone Jul 20, 2025
@tomkralidis tomkralidis added bug Something isn't working OGC API - Coverages OGC API - Coverages labels Jul 20, 2025
@geographika
Copy link
Contributor Author

The issue was that the NumPy tolist function originally used converts to to the nearest compatible builtin Python type, however it does not convert nan to Python None as nan is a valid Python float:

>>> type(float('nan'))
<class 'float'>

So any nan values are left as-is, and persisted to the JSON output causing invalid JSON.

import numpy as np
arr = np.array([1.0, np.nan, 3.0])
arr.tolist()
[1.0, nan, 3.0]

This PR ensures nan are converted to None and then when converted to JSON are returned as null. NaN is not a valid JSON value. tolist takes care of converting other NumPy types such as np.uint32(2) (so no changes are required in json_serial in util.py.

cc @tomkralidis

@geographika
Copy link
Contributor Author

Maybe estonia_light.tif can be added as a test (it is 11KB).

@tomkralidis tomkralidis merged commit e8aef58 into geopython:master Jul 21, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working OGC API - Coverages OGC API - Coverages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants